import ast
import os

import bean.param as param
import beam_bridge_model as type1
import cable_stayed_bridge_model as type3
from datetime import datetime

def analysis(req: param.AnalysisReq, data: param.CaeData):
    print("analysis 有限元模型开始,code:{}".format(req.bridgeCode))
    bridgeName = req.bridgeName
    bridgeCode = req.bridgeCode
    bridgeType = req.bridgeType


    # 文件夹路径
    image_dir = f'images/{bridgeCode}/'
    skeleton_dir = f'skeleton/{bridgeCode}/'  # skeleton存储路径
    # 不存在则创建
    create_dir_if_not_exists(image_dir)
    create_dir_if_not_exists(skeleton_dir)
    # 文件路径+名字前缀
    format_time=datetime.now().strftime('%Y%m%d%H%M%S')

    image_path = f'{image_dir}{bridgeCode}_{format_time}'
    skeleton_path = f'{skeleton_dir}{bridgeCode}_{format_time}'

    if bridgeType == 1:
        # 梁桥
        return type1.beam_bridge_input(ast.literal_eval(data.spans),
                                       float(data.width),
                                       bridgeCode, image_path, skeleton_path,
                                       ast.literal_eval(data.column_num),
                                       float(data.column_h),
                                       int(data.beam_type),
                                       int(data.bridge_year),
                                       bridgeName)


    elif bridgeType == 3:
        # 斜拉桥
        return type3.cable_stayed_bridge_input(ast.literal_eval(data.spans),
                                               float(data.width),
                                               int(data.cable_plane_num),
                                               int(data.cable_num),
                                               int(data.tower_vertical_type),
                                               int(data.tower_horizontal_type),
                                               float(data.tower_h_ratio),
                                               float(data.tower_h_1),
                                               int(data.auxi_col_num),
                                               bridgeCode, image_path, skeleton_path,
                                               int(data.girder_type),
                                               int(data.girder_mat),
                                               int(data.bridge_year),
                                               req.bridgeName)


def create_dir_if_not_exists(dir_path):
    """
    如果指定路径的文件夹不存在，则创建文件夹。
    :param dir_path: 文件夹路径
    :return: 无
    """
    if not os.path.exists(dir_path):
        os.makedirs(dir_path)
        print(f"文件夹 {dir_path} 已创建。")
    else:
        print(f"文件夹 {dir_path} 已存在。")
